
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 06/10/2012 23:51:48
-- Generated from EDMX file: D:\dipatcher\Repository\Model.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [Diplom];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_PersonGroupPersonGroup_PersonGroup]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PersonGroupPersonGroup] DROP CONSTRAINT [FK_PersonGroupPersonGroup_PersonGroup];
GO
IF OBJECT_ID(N'[dbo].[FK_PersonGroupPersonGroup_Group1]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PersonGroupPersonGroup] DROP CONSTRAINT [FK_PersonGroupPersonGroup_Group1];
GO
IF OBJECT_ID(N'[dbo].[FK_EventPlace]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[EventSet] DROP CONSTRAINT [FK_EventPlace];
GO
IF OBJECT_ID(N'[dbo].[FK_PersonGroupEvent]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[EventSet] DROP CONSTRAINT [FK_PersonGroupEvent];
GO
IF OBJECT_ID(N'[dbo].[FK_PersonGroupEvent1]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[EventSet] DROP CONSTRAINT [FK_PersonGroupEvent1];
GO
IF OBJECT_ID(N'[dbo].[FK_PlaceEquipment]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[EquipmentSet] DROP CONSTRAINT [FK_PlaceEquipment];
GO
IF OBJECT_ID(N'[dbo].[FK_EquipmentTypeEquipment]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[EquipmentSet] DROP CONSTRAINT [FK_EquipmentTypeEquipment];
GO
IF OBJECT_ID(N'[dbo].[FK_PlaceTypePlace]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PlaceSet] DROP CONSTRAINT [FK_PlaceTypePlace];
GO
IF OBJECT_ID(N'[dbo].[FK_UserPerson]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PersonSet] DROP CONSTRAINT [FK_UserPerson];
GO
IF OBJECT_ID(N'[dbo].[FK_PersonGroupTypePersonGroup]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PersonGroupSet] DROP CONSTRAINT [FK_PersonGroupTypePersonGroup];
GO
IF OBJECT_ID(N'[dbo].[FK_UserMessage]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[MessageSet] DROP CONSTRAINT [FK_UserMessage];
GO
IF OBJECT_ID(N'[dbo].[FK_UserMessage1]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[MessageSet] DROP CONSTRAINT [FK_UserMessage1];
GO
IF OBJECT_ID(N'[dbo].[FK_LocationPlace]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PlaceSet] DROP CONSTRAINT [FK_LocationPlace];
GO
IF OBJECT_ID(N'[dbo].[FK_EventEquipment_Event]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[EventEquipment] DROP CONSTRAINT [FK_EventEquipment_Event];
GO
IF OBJECT_ID(N'[dbo].[FK_EventEquipment_Equipment]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[EventEquipment] DROP CONSTRAINT [FK_EventEquipment_Equipment];
GO
IF OBJECT_ID(N'[dbo].[FK_PersonPersonGroup_Person]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PersonPersonGroup] DROP CONSTRAINT [FK_PersonPersonGroup_Person];
GO
IF OBJECT_ID(N'[dbo].[FK_PersonPersonGroup_PersonGroup]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PersonPersonGroup] DROP CONSTRAINT [FK_PersonPersonGroup_PersonGroup];
GO
IF OBJECT_ID(N'[dbo].[FK_EventGroupEvent_EventGroup]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[EventGroupEvent] DROP CONSTRAINT [FK_EventGroupEvent_EventGroup];
GO
IF OBJECT_ID(N'[dbo].[FK_EventGroupEvent_Event]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[EventGroupEvent] DROP CONSTRAINT [FK_EventGroupEvent_Event];
GO
IF OBJECT_ID(N'[dbo].[FK_EventGroupTypeEventGroup]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[EventGroupSet] DROP CONSTRAINT [FK_EventGroupTypeEventGroup];
GO
IF OBJECT_ID(N'[dbo].[FK_EventGroupEventGroup_EventGroup]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[EventGroupEventGroup] DROP CONSTRAINT [FK_EventGroupEventGroup_EventGroup];
GO
IF OBJECT_ID(N'[dbo].[FK_EventGroupEventGroup_EventGroup1]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[EventGroupEventGroup] DROP CONSTRAINT [FK_EventGroupEventGroup_EventGroup1];
GO
IF OBJECT_ID(N'[dbo].[FK_PlacePersonGroup_Place]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PlacePersonGroup] DROP CONSTRAINT [FK_PlacePersonGroup_Place];
GO
IF OBJECT_ID(N'[dbo].[FK_PlacePersonGroup_PersonGroup]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PlacePersonGroup] DROP CONSTRAINT [FK_PlacePersonGroup_PersonGroup];
GO
IF OBJECT_ID(N'[dbo].[FK_RoleUser]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserSet] DROP CONSTRAINT [FK_RoleUser];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[EventSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[EventSet];
GO
IF OBJECT_ID(N'[dbo].[PlaceSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PlaceSet];
GO
IF OBJECT_ID(N'[dbo].[PersonSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PersonSet];
GO
IF OBJECT_ID(N'[dbo].[PersonGroupSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PersonGroupSet];
GO
IF OBJECT_ID(N'[dbo].[EquipmentSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[EquipmentSet];
GO
IF OBJECT_ID(N'[dbo].[EquipmentTypeSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[EquipmentTypeSet];
GO
IF OBJECT_ID(N'[dbo].[LocationSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[LocationSet];
GO
IF OBJECT_ID(N'[dbo].[EventGroupTypeSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[EventGroupTypeSet];
GO
IF OBJECT_ID(N'[dbo].[PlaceTypeSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PlaceTypeSet];
GO
IF OBJECT_ID(N'[dbo].[UserSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[UserSet];
GO
IF OBJECT_ID(N'[dbo].[PersonGroupTypeSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PersonGroupTypeSet];
GO
IF OBJECT_ID(N'[dbo].[MessageSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[MessageSet];
GO
IF OBJECT_ID(N'[dbo].[EventGroupSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[EventGroupSet];
GO
IF OBJECT_ID(N'[dbo].[RoleSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[RoleSet];
GO
IF OBJECT_ID(N'[dbo].[PersonGroupPersonGroup]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PersonGroupPersonGroup];
GO
IF OBJECT_ID(N'[dbo].[EventEquipment]', 'U') IS NOT NULL
    DROP TABLE [dbo].[EventEquipment];
GO
IF OBJECT_ID(N'[dbo].[PersonPersonGroup]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PersonPersonGroup];
GO
IF OBJECT_ID(N'[dbo].[EventGroupEvent]', 'U') IS NOT NULL
    DROP TABLE [dbo].[EventGroupEvent];
GO
IF OBJECT_ID(N'[dbo].[EventGroupEventGroup]', 'U') IS NOT NULL
    DROP TABLE [dbo].[EventGroupEventGroup];
GO
IF OBJECT_ID(N'[dbo].[PlacePersonGroup]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PlacePersonGroup];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'EventSet'
CREATE TABLE [dbo].[EventSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [StartTime] datetime  NOT NULL,
    [EndTime] datetime  NOT NULL,
    [Place_Id] int  NULL,
    [OrganizePersonGroup_Id] int  NULL,
    [ParticipatePersonGroup_Id] int  NULL
);
GO

-- Creating table 'PlaceSet'
CREATE TABLE [dbo].[PlaceSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NULL,
    [Capacity] int  NULL,
    [PlaceType_Id] int  NOT NULL,
    [Location_Id] int  NULL
);
GO

-- Creating table 'PersonSet'
CREATE TABLE [dbo].[PersonSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [FirstName] nvarchar(max)  NOT NULL,
    [LastName] nvarchar(max)  NOT NULL,
    [Patronymic] nvarchar(max)  NULL,
    [Birthday] datetime  NOT NULL,
    [Phone] nvarchar(max)  NULL,
    [User_Id] int  NULL
);
GO

-- Creating table 'PersonGroupSet'
CREATE TABLE [dbo].[PersonGroupSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NULL,
    [PersonGroupType_Id] int  NOT NULL
);
GO

-- Creating table 'EquipmentSet'
CREATE TABLE [dbo].[EquipmentSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NULL,
    [Place_Id] int  NULL,
    [EquipmentType_Id] int  NULL
);
GO

-- Creating table 'EquipmentTypeSet'
CREATE TABLE [dbo].[EquipmentTypeSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NULL
);
GO

-- Creating table 'LocationSet'
CREATE TABLE [dbo].[LocationSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NULL,
    [Address] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'EventGroupTypeSet'
CREATE TABLE [dbo].[EventGroupTypeSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NULL
);
GO

-- Creating table 'PlaceTypeSet'
CREATE TABLE [dbo].[PlaceTypeSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NULL
);
GO

-- Creating table 'UserSet'
CREATE TABLE [dbo].[UserSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [PasswordHash] nvarchar(max)  NOT NULL,
    [Role_Id] int  NOT NULL
);
GO

-- Creating table 'PersonGroupTypeSet'
CREATE TABLE [dbo].[PersonGroupTypeSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NULL
);
GO

-- Creating table 'MessageSet'
CREATE TABLE [dbo].[MessageSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [FromUser_Id] int  NOT NULL,
    [ToUser_Id] int  NOT NULL
);
GO

-- Creating table 'EventGroupSet'
CREATE TABLE [dbo].[EventGroupSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NULL,
    [EventGroupType_Id] int  NOT NULL
);
GO

-- Creating table 'RoleSet'
CREATE TABLE [dbo].[RoleSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NULL
);
GO

-- Creating table 'PersonGroupPersonGroup'
CREATE TABLE [dbo].[PersonGroupPersonGroup] (
    [ParentPersonGroup_Id] int  NOT NULL,
    [ChildPersonGroup_Id] int  NOT NULL
);
GO

-- Creating table 'EventEquipment'
CREATE TABLE [dbo].[EventEquipment] (
    [Event_Id] int  NOT NULL,
    [Equipment_Id] int  NOT NULL
);
GO

-- Creating table 'PersonPersonGroup'
CREATE TABLE [dbo].[PersonPersonGroup] (
    [Person_Id] int  NOT NULL,
    [PersonGroup_Id] int  NOT NULL
);
GO

-- Creating table 'EventGroupEvent'
CREATE TABLE [dbo].[EventGroupEvent] (
    [EventGroup_Id] int  NOT NULL,
    [Event_Id] int  NOT NULL
);
GO

-- Creating table 'EventGroupEventGroup'
CREATE TABLE [dbo].[EventGroupEventGroup] (
    [ChildEventGroup_Id] int  NOT NULL,
    [ParentEventGroup_Id] int  NOT NULL
);
GO

-- Creating table 'PlacePersonGroup'
CREATE TABLE [dbo].[PlacePersonGroup] (
    [Place_Id] int  NOT NULL,
    [ResponsiblePersonGroup_Id] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'EventSet'
ALTER TABLE [dbo].[EventSet]
ADD CONSTRAINT [PK_EventSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'PlaceSet'
ALTER TABLE [dbo].[PlaceSet]
ADD CONSTRAINT [PK_PlaceSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'PersonSet'
ALTER TABLE [dbo].[PersonSet]
ADD CONSTRAINT [PK_PersonSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'PersonGroupSet'
ALTER TABLE [dbo].[PersonGroupSet]
ADD CONSTRAINT [PK_PersonGroupSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'EquipmentSet'
ALTER TABLE [dbo].[EquipmentSet]
ADD CONSTRAINT [PK_EquipmentSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'EquipmentTypeSet'
ALTER TABLE [dbo].[EquipmentTypeSet]
ADD CONSTRAINT [PK_EquipmentTypeSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'LocationSet'
ALTER TABLE [dbo].[LocationSet]
ADD CONSTRAINT [PK_LocationSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'EventGroupTypeSet'
ALTER TABLE [dbo].[EventGroupTypeSet]
ADD CONSTRAINT [PK_EventGroupTypeSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'PlaceTypeSet'
ALTER TABLE [dbo].[PlaceTypeSet]
ADD CONSTRAINT [PK_PlaceTypeSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'UserSet'
ALTER TABLE [dbo].[UserSet]
ADD CONSTRAINT [PK_UserSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'PersonGroupTypeSet'
ALTER TABLE [dbo].[PersonGroupTypeSet]
ADD CONSTRAINT [PK_PersonGroupTypeSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'MessageSet'
ALTER TABLE [dbo].[MessageSet]
ADD CONSTRAINT [PK_MessageSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'EventGroupSet'
ALTER TABLE [dbo].[EventGroupSet]
ADD CONSTRAINT [PK_EventGroupSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'RoleSet'
ALTER TABLE [dbo].[RoleSet]
ADD CONSTRAINT [PK_RoleSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [ParentPersonGroup_Id], [ChildPersonGroup_Id] in table 'PersonGroupPersonGroup'
ALTER TABLE [dbo].[PersonGroupPersonGroup]
ADD CONSTRAINT [PK_PersonGroupPersonGroup]
    PRIMARY KEY NONCLUSTERED ([ParentPersonGroup_Id], [ChildPersonGroup_Id] ASC);
GO

-- Creating primary key on [Event_Id], [Equipment_Id] in table 'EventEquipment'
ALTER TABLE [dbo].[EventEquipment]
ADD CONSTRAINT [PK_EventEquipment]
    PRIMARY KEY NONCLUSTERED ([Event_Id], [Equipment_Id] ASC);
GO

-- Creating primary key on [Person_Id], [PersonGroup_Id] in table 'PersonPersonGroup'
ALTER TABLE [dbo].[PersonPersonGroup]
ADD CONSTRAINT [PK_PersonPersonGroup]
    PRIMARY KEY NONCLUSTERED ([Person_Id], [PersonGroup_Id] ASC);
GO

-- Creating primary key on [EventGroup_Id], [Event_Id] in table 'EventGroupEvent'
ALTER TABLE [dbo].[EventGroupEvent]
ADD CONSTRAINT [PK_EventGroupEvent]
    PRIMARY KEY NONCLUSTERED ([EventGroup_Id], [Event_Id] ASC);
GO

-- Creating primary key on [ChildEventGroup_Id], [ParentEventGroup_Id] in table 'EventGroupEventGroup'
ALTER TABLE [dbo].[EventGroupEventGroup]
ADD CONSTRAINT [PK_EventGroupEventGroup]
    PRIMARY KEY NONCLUSTERED ([ChildEventGroup_Id], [ParentEventGroup_Id] ASC);
GO

-- Creating primary key on [Place_Id], [ResponsiblePersonGroup_Id] in table 'PlacePersonGroup'
ALTER TABLE [dbo].[PlacePersonGroup]
ADD CONSTRAINT [PK_PlacePersonGroup]
    PRIMARY KEY NONCLUSTERED ([Place_Id], [ResponsiblePersonGroup_Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [ParentPersonGroup_Id] in table 'PersonGroupPersonGroup'
ALTER TABLE [dbo].[PersonGroupPersonGroup]
ADD CONSTRAINT [FK_PersonGroupPersonGroup_PersonGroup]
    FOREIGN KEY ([ParentPersonGroup_Id])
    REFERENCES [dbo].[PersonGroupSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [ChildPersonGroup_Id] in table 'PersonGroupPersonGroup'
ALTER TABLE [dbo].[PersonGroupPersonGroup]
ADD CONSTRAINT [FK_PersonGroupPersonGroup_Group1]
    FOREIGN KEY ([ChildPersonGroup_Id])
    REFERENCES [dbo].[PersonGroupSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PersonGroupPersonGroup_Group1'
CREATE INDEX [IX_FK_PersonGroupPersonGroup_Group1]
ON [dbo].[PersonGroupPersonGroup]
    ([ChildPersonGroup_Id]);
GO

-- Creating foreign key on [Place_Id] in table 'EventSet'
ALTER TABLE [dbo].[EventSet]
ADD CONSTRAINT [FK_EventPlace]
    FOREIGN KEY ([Place_Id])
    REFERENCES [dbo].[PlaceSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_EventPlace'
CREATE INDEX [IX_FK_EventPlace]
ON [dbo].[EventSet]
    ([Place_Id]);
GO

-- Creating foreign key on [OrganizePersonGroup_Id] in table 'EventSet'
ALTER TABLE [dbo].[EventSet]
ADD CONSTRAINT [FK_PersonGroupEvent]
    FOREIGN KEY ([OrganizePersonGroup_Id])
    REFERENCES [dbo].[PersonGroupSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PersonGroupEvent'
CREATE INDEX [IX_FK_PersonGroupEvent]
ON [dbo].[EventSet]
    ([OrganizePersonGroup_Id]);
GO

-- Creating foreign key on [ParticipatePersonGroup_Id] in table 'EventSet'
ALTER TABLE [dbo].[EventSet]
ADD CONSTRAINT [FK_PersonGroupEvent1]
    FOREIGN KEY ([ParticipatePersonGroup_Id])
    REFERENCES [dbo].[PersonGroupSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PersonGroupEvent1'
CREATE INDEX [IX_FK_PersonGroupEvent1]
ON [dbo].[EventSet]
    ([ParticipatePersonGroup_Id]);
GO

-- Creating foreign key on [Place_Id] in table 'EquipmentSet'
ALTER TABLE [dbo].[EquipmentSet]
ADD CONSTRAINT [FK_PlaceEquipment]
    FOREIGN KEY ([Place_Id])
    REFERENCES [dbo].[PlaceSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PlaceEquipment'
CREATE INDEX [IX_FK_PlaceEquipment]
ON [dbo].[EquipmentSet]
    ([Place_Id]);
GO

-- Creating foreign key on [EquipmentType_Id] in table 'EquipmentSet'
ALTER TABLE [dbo].[EquipmentSet]
ADD CONSTRAINT [FK_EquipmentTypeEquipment]
    FOREIGN KEY ([EquipmentType_Id])
    REFERENCES [dbo].[EquipmentTypeSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_EquipmentTypeEquipment'
CREATE INDEX [IX_FK_EquipmentTypeEquipment]
ON [dbo].[EquipmentSet]
    ([EquipmentType_Id]);
GO

-- Creating foreign key on [PlaceType_Id] in table 'PlaceSet'
ALTER TABLE [dbo].[PlaceSet]
ADD CONSTRAINT [FK_PlaceTypePlace]
    FOREIGN KEY ([PlaceType_Id])
    REFERENCES [dbo].[PlaceTypeSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PlaceTypePlace'
CREATE INDEX [IX_FK_PlaceTypePlace]
ON [dbo].[PlaceSet]
    ([PlaceType_Id]);
GO

-- Creating foreign key on [User_Id] in table 'PersonSet'
ALTER TABLE [dbo].[PersonSet]
ADD CONSTRAINT [FK_UserPerson]
    FOREIGN KEY ([User_Id])
    REFERENCES [dbo].[UserSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserPerson'
CREATE INDEX [IX_FK_UserPerson]
ON [dbo].[PersonSet]
    ([User_Id]);
GO

-- Creating foreign key on [PersonGroupType_Id] in table 'PersonGroupSet'
ALTER TABLE [dbo].[PersonGroupSet]
ADD CONSTRAINT [FK_PersonGroupTypePersonGroup]
    FOREIGN KEY ([PersonGroupType_Id])
    REFERENCES [dbo].[PersonGroupTypeSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PersonGroupTypePersonGroup'
CREATE INDEX [IX_FK_PersonGroupTypePersonGroup]
ON [dbo].[PersonGroupSet]
    ([PersonGroupType_Id]);
GO

-- Creating foreign key on [FromUser_Id] in table 'MessageSet'
ALTER TABLE [dbo].[MessageSet]
ADD CONSTRAINT [FK_UserMessage]
    FOREIGN KEY ([FromUser_Id])
    REFERENCES [dbo].[UserSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserMessage'
CREATE INDEX [IX_FK_UserMessage]
ON [dbo].[MessageSet]
    ([FromUser_Id]);
GO

-- Creating foreign key on [ToUser_Id] in table 'MessageSet'
ALTER TABLE [dbo].[MessageSet]
ADD CONSTRAINT [FK_UserMessage1]
    FOREIGN KEY ([ToUser_Id])
    REFERENCES [dbo].[UserSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserMessage1'
CREATE INDEX [IX_FK_UserMessage1]
ON [dbo].[MessageSet]
    ([ToUser_Id]);
GO

-- Creating foreign key on [Location_Id] in table 'PlaceSet'
ALTER TABLE [dbo].[PlaceSet]
ADD CONSTRAINT [FK_LocationPlace]
    FOREIGN KEY ([Location_Id])
    REFERENCES [dbo].[LocationSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_LocationPlace'
CREATE INDEX [IX_FK_LocationPlace]
ON [dbo].[PlaceSet]
    ([Location_Id]);
GO

-- Creating foreign key on [Event_Id] in table 'EventEquipment'
ALTER TABLE [dbo].[EventEquipment]
ADD CONSTRAINT [FK_EventEquipment_Event]
    FOREIGN KEY ([Event_Id])
    REFERENCES [dbo].[EventSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Equipment_Id] in table 'EventEquipment'
ALTER TABLE [dbo].[EventEquipment]
ADD CONSTRAINT [FK_EventEquipment_Equipment]
    FOREIGN KEY ([Equipment_Id])
    REFERENCES [dbo].[EquipmentSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_EventEquipment_Equipment'
CREATE INDEX [IX_FK_EventEquipment_Equipment]
ON [dbo].[EventEquipment]
    ([Equipment_Id]);
GO

-- Creating foreign key on [Person_Id] in table 'PersonPersonGroup'
ALTER TABLE [dbo].[PersonPersonGroup]
ADD CONSTRAINT [FK_PersonPersonGroup_Person]
    FOREIGN KEY ([Person_Id])
    REFERENCES [dbo].[PersonSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [PersonGroup_Id] in table 'PersonPersonGroup'
ALTER TABLE [dbo].[PersonPersonGroup]
ADD CONSTRAINT [FK_PersonPersonGroup_PersonGroup]
    FOREIGN KEY ([PersonGroup_Id])
    REFERENCES [dbo].[PersonGroupSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PersonPersonGroup_PersonGroup'
CREATE INDEX [IX_FK_PersonPersonGroup_PersonGroup]
ON [dbo].[PersonPersonGroup]
    ([PersonGroup_Id]);
GO

-- Creating foreign key on [EventGroup_Id] in table 'EventGroupEvent'
ALTER TABLE [dbo].[EventGroupEvent]
ADD CONSTRAINT [FK_EventGroupEvent_EventGroup]
    FOREIGN KEY ([EventGroup_Id])
    REFERENCES [dbo].[EventGroupSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Event_Id] in table 'EventGroupEvent'
ALTER TABLE [dbo].[EventGroupEvent]
ADD CONSTRAINT [FK_EventGroupEvent_Event]
    FOREIGN KEY ([Event_Id])
    REFERENCES [dbo].[EventSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_EventGroupEvent_Event'
CREATE INDEX [IX_FK_EventGroupEvent_Event]
ON [dbo].[EventGroupEvent]
    ([Event_Id]);
GO

-- Creating foreign key on [EventGroupType_Id] in table 'EventGroupSet'
ALTER TABLE [dbo].[EventGroupSet]
ADD CONSTRAINT [FK_EventGroupTypeEventGroup]
    FOREIGN KEY ([EventGroupType_Id])
    REFERENCES [dbo].[EventGroupTypeSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_EventGroupTypeEventGroup'
CREATE INDEX [IX_FK_EventGroupTypeEventGroup]
ON [dbo].[EventGroupSet]
    ([EventGroupType_Id]);
GO

-- Creating foreign key on [ChildEventGroup_Id] in table 'EventGroupEventGroup'
ALTER TABLE [dbo].[EventGroupEventGroup]
ADD CONSTRAINT [FK_EventGroupEventGroup_EventGroup]
    FOREIGN KEY ([ChildEventGroup_Id])
    REFERENCES [dbo].[EventGroupSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [ParentEventGroup_Id] in table 'EventGroupEventGroup'
ALTER TABLE [dbo].[EventGroupEventGroup]
ADD CONSTRAINT [FK_EventGroupEventGroup_EventGroup1]
    FOREIGN KEY ([ParentEventGroup_Id])
    REFERENCES [dbo].[EventGroupSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_EventGroupEventGroup_EventGroup1'
CREATE INDEX [IX_FK_EventGroupEventGroup_EventGroup1]
ON [dbo].[EventGroupEventGroup]
    ([ParentEventGroup_Id]);
GO

-- Creating foreign key on [Place_Id] in table 'PlacePersonGroup'
ALTER TABLE [dbo].[PlacePersonGroup]
ADD CONSTRAINT [FK_PlacePersonGroup_Place]
    FOREIGN KEY ([Place_Id])
    REFERENCES [dbo].[PlaceSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [ResponsiblePersonGroup_Id] in table 'PlacePersonGroup'
ALTER TABLE [dbo].[PlacePersonGroup]
ADD CONSTRAINT [FK_PlacePersonGroup_PersonGroup]
    FOREIGN KEY ([ResponsiblePersonGroup_Id])
    REFERENCES [dbo].[PersonGroupSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PlacePersonGroup_PersonGroup'
CREATE INDEX [IX_FK_PlacePersonGroup_PersonGroup]
ON [dbo].[PlacePersonGroup]
    ([ResponsiblePersonGroup_Id]);
GO

-- Creating foreign key on [Role_Id] in table 'UserSet'
ALTER TABLE [dbo].[UserSet]
ADD CONSTRAINT [FK_RoleUser]
    FOREIGN KEY ([Role_Id])
    REFERENCES [dbo].[RoleSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_RoleUser'
CREATE INDEX [IX_FK_RoleUser]
ON [dbo].[UserSet]
    ([Role_Id]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------